﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using CarLoanSystem.DAL;

namespace CarLoanSystem
{
    public partial class frmMemberDetail : Form
    {
        private static CustomerItem cusItem;
        private static EmployeeItem empItem;
        public frmMemberDetail()
        {
            InitializeComponent();
        }

        public frmMemberDetail(int id,string mode,EmployeeItem item)
        {
            InitializeComponent();
            empItem = item;
            cusItem = DAL.CustomerInfoDAL.LoadCustomer(id);
            this.txtAddress.Text = cusItem.CUSTOMER_ADDRESS;
            this.txtAddressWork.Text = cusItem.CUSTOMER_WORK_ADDRESS;
            this.txtAge.Text = cusItem.CUSTOMER_AGE.ToString();
            this.txtBlackList.Text = cusItem.CUSTOMER_BLACK_LIST.ToString();
            this.txtJob.Text = cusItem.CUSTOMER_JOB;
            this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;
            this.txtName.Text = cusItem.CUSTOMER_NAME;
            this.txtPresonalId.Text = cusItem.CUSTOMER_PERSONAL_ID;
            this.txtTel.Text = cusItem.CUSTOMER_TEL;
            this.txtTelWork.Text = cusItem.CUSTOMER_WORK_TEL;
            this.txtUserName.Text = cusItem.USERNAME;
            this.txtSalary.Text = cusItem.CUSTOMER_SARARY.ToString();
            genGridview();
            this.grdView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmCusDept frmCusDept = new frmCusDept(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()),"Edit");
                frmCusDept.ShowDialog();
                this.grdView.DataSource = LoadDeptCustomer();
            }
        }

        private void btnAddDept_Click(object sender, EventArgs e)
        {
            frmCusDept frmCusDept = new frmCusDept(cusItem.CUSTOMER_ID,"Add");
            frmCusDept.ShowDialog();
            this.grdView.DataSource = LoadDeptCustomer();
        }

        private void genGridview()
        {
            // gen Grideview
            grdView.AutoGenerateColumns = false;

            //Set Columns Count
            grdView.ColumnCount = 5;

            grdView.Columns[0].Width = 50;

            //Add Columns
            grdView.Columns[1].Name = "CUSTOMER_DEBT_ID";
            grdView.Columns[1].HeaderText = "ลำดับ";
            grdView.Columns[1].DataPropertyName = "CUSTOMER_DEBT_ID";
            grdView.Columns[1].Width = 35;

            grdView.Columns[2].Name = "DEBT_AMOUNT";
            grdView.Columns[2].HeaderText = "จำนวนเงิน";
            grdView.Columns[2].DataPropertyName = "DEBT_AMOUNT";
            grdView.Columns[2].Width = 100;

            grdView.Columns[3].Name = "DEBT_DETAIL";
            grdView.Columns[3].HeaderText = "รายละเอียด";
            grdView.Columns[3].DataPropertyName = "DEBT_DETAIL";
            grdView.Columns[3].Width = 250;


            grdView.Columns[4].Name = "CUSTOMER_DEBT_STATUS";
            grdView.Columns[4].HeaderText = "สถานะ";
            grdView.Columns[4].DataPropertyName = "CUSTOMER_DEBT_STATUS";
            grdView.Columns[4].Width = 65;
            // end gen gridview

            this.grdView.DataSource = LoadDeptCustomer();
        }

        private DataTable LoadDeptCustomer() 
        {
            DataTable dt = CustomerDebtInfoDAL.LoadCusDeptAll(cusItem.CUSTOMER_ID);
            return dt;
        }
    }
}
